package pers.qianyu.auth.security;

import org.springframework.security.authentication.*;
import org.springframework.security.authentication.dao.*;
import org.springframework.security.core.*;
import org.springframework.security.core.userdetails.*;
import org.springframework.security.crypto.bcrypt.*;

public class JwtAuthenticationProvider extends DaoAuthenticationProvider {
    public JwtAuthenticationProvider(UserDetailsService userDetailsService) {
        setUserDetailsService(userDetailsService);
    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken auth)
            throws AuthenticationException {
        String messageKey = "AbstractUserDetailsAuthenticationProvider.badCredentials";
        if (auth.getCredentials() == null) {
            logger.debug("密码为空");
            throw new BadCredentialsException(messages.getMessage(messageKey, "用户名或密码错误"));
        }
        String presentedPassword = auth.getCredentials().toString();
        if (!BCrypt.checkpw(presentedPassword, userDetails.getPassword())) {
            logger.debug("密码错误");
            throw new BadCredentialsException(messages.getMessage(messageKey, "用户名或密码错误"));
        }
    }
}